package com.pylc.model.dao.impl;

import com.pylc.model.dao.GoodsCategoryDao;
import com.pylc.model.entity.GoodsCategory;
import com.pylc.model.entity.StoreCategory;
import org.compass.core.util.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Lindsey
 */
@Repository
public class GoodsCategoryDaoImpl extends BaseDaoImpl<GoodsCategory, Long> implements GoodsCategoryDao {
    @Override
    public Set<GoodsCategory> getRoot() {
        String hql = "from GoodsCategory c where c.parent is null";
        return new HashSet<GoodsCategory>((List<GoodsCategory>) getHibernateTemplate().find(hql));
    }

    @Override
    public Set<GoodsCategory> getChildren(GoodsCategory goodsCategory) {
        String hql = "from GoodsCategory c join fetch c.children where c.id = ?";
        List list = getHibernateTemplate().find(hql, goodsCategory.getId());
        if (!CollectionUtils.isEmpty(list)) {
            goodsCategory = (GoodsCategory) list.get(0);
            return goodsCategory.getChildren();
        }
        return null;
    }

    @Override
    public boolean isNameUnique(String name) {
        return isUnique("name", name);
    }
}
